<?php
require_once 'database/MySQL.php';

class Product {
	
	
	public function __construct(){
		
	}
	
	/**
	 * Returns an arrayObject containing all the products
	 */
	public function getAllProducts(){
		$query = "SELECT * FROM product";
		return MySQL::getArrayObject($query);
	}
	
	/**
	 * Return a product in an ArrayObject based on its id
	 * 
	 * @param int $id	-	the id of the product
	 */
	public function getProduct($id){
		$query = sprintf("	SELECT *
							FROM product
							WHERE id = %d",
							mysql_real_escape_string($id));
		return MySQL::getArrayObject($query);
	}
	
	/**
	 * Returns a product based on a list of tags
	 */
	public function getProductsByTags($idArr){
		$ids = "";
		foreach($idArr as $id){
			$ids .= "'". $id . "',";
		}
		$ids = substr($ids, 0, -1);
		$query = sprintf("	SELECT 	product.id AS 'productid',
									product.name AS 'productname',
									product.price AS 'productprice',
									tag.id AS 'tagid', 
									tag.name AS 'tagname'
							FROM product INNER JOIN tag ON product.tag = tag.id
							WHERE tag.name IN(%s)", 
							$ids);
		return MySQL::getArrayObject($query);
	}
	
}